﻿using Company.Plc;
using Company.Shell.Interfaces;


namespace Company.Shell.Services
{
    public class CollectManager : ICollectManager
    {
        private CancellationTokenSource cts = new();
        public IRoomManager RoomManager {  get;  }
        public IPlc Plc { get; }

        public CollectManager(IRoomManager roomManager, IPlc plc)
        {
            RoomManager = roomManager;
            Plc = plc;
        }
        public void Start()
        {
            cts = new();
            Task.Run(() =>
            {
                while (!cts.IsCancellationRequested)
                {
                    foreach (var room in RoomManager.Rooms.ToList())
                    {
                        if (cts.IsCancellationRequested)
                        
                            break;
                            var array = Plc.Read(room.Address.ToString(), 100);
                            room.Resolve(array);
                            Thread.Sleep(100);
                            
                    }  
                }
            });
        }

        public void Stop() => cts.Cancel();
       
    }
}
